﻿/*******************************************************************
 * DbFirstAid - A Bandaging / First Aid Plugin
 * Plugin Based on Source code from DbWarlock and Alpha First Aider Plugin
 * 
 * 
 * Change Log
 * 
 * [0.1 - 21-Jul-2011]
 * -Plugin Created
 * -Suport First aid trough a gui to manage skill!
 * 
 * 
 * 
 *  
 */

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;

using Styx;
using Styx.Helpers;
using Styx.Logic;
using Styx.Logic.Combat;
using Styx.Plugins.PluginClass;
using Styx.WoWInternals;
using Styx.WoWInternals.WoWObjects;


namespace DbFirstAid
{
    public class DBFirstAid : HBPlugin
    {
        #region PluginData

        public override string Name { get { return "DBFirstAid"; } }
        public override string Author { get { return "DarkBen"; } }
        public override Version Version { get { return _version; } }
        private readonly Version _version = new Version(1,0);
        public override bool WantButton { get { return true; } }
        public override void OnButtonPress() { if (!ConfigForm.Visible)ConfigForm.Show(); else ConfigForm.Focus(); }
        /// <summary>
        /// Configuration Form
        /// </summary>
        private DBFirstAidConfig _myForm;

        public DBFirstAidConfig ConfigForm
        {
            get
            {
                if (_myForm == null || _myForm.IsDisposed)
                    _myForm = new DBFirstAidConfig();
                return _myForm;
            }
        }

        static public DbFirstAidConfigSettings config = new DbFirstAidConfigSettings();

        #endregion

        #region PluginConstant
        public WoWSkill _firstAid = ObjectManager.Me.GetSkill(SkillLine.FirstAid);

        public enum bandageType
        {
            None=0,
            Normal=1,
            Heavy=2
        }

        private List<uint> bandageEID = new List<uint>()
        {
            1251, 2581, 3530, 3531, 6450, 6451, 8544, 8545, 14529, 14530, 21990, 21991, 34721, 34722
        };


        WoWSpell linen1 = WoWSpell.FromId(3275);
        WoWSpell linen2 = WoWSpell.FromId(3276);
        WoWSpell woolen1 = WoWSpell.FromId(3277);
        WoWSpell woolen2 = WoWSpell.FromId(3278);
        WoWSpell silk1 = WoWSpell.FromId(7928);
        WoWSpell silk2 = WoWSpell.FromId(7929);
        WoWSpell mage1 = WoWSpell.FromId(10840);
        WoWSpell mage2 = WoWSpell.FromId(10841);
        WoWSpell rune1 = WoWSpell.FromId(18629);
        WoWSpell rune2 = WoWSpell.FromId(18630);
        WoWSpell nether1 = WoWSpell.FromId(27032);
        WoWSpell nether2 = WoWSpell.FromId(27033);
        WoWSpell frost1 = WoWSpell.FromId(45545);
        WoWSpell frost2 = WoWSpell.FromId(45546);
        WoWSpell ember1 = WoWSpell.FromId(74556);
        WoWSpell ember2 = WoWSpell.FromId(74557);
        WoWSpell ember3 = WoWSpell.FromId(88893); //dense
        WoWSpell ember4 = WoWSpell.FromId(74558); //

        #endregion

        private WoWItem HaveItemCheck(List<uint> listId)
        {
            foreach (WoWItem item in ObjectManager.GetObjectsOfType<WoWItem>(false))
            {
                if (listId.Contains(item.Entry))
                {
                    return item;
                }
            }
            return null;
        }

        static int NumOfItemsInBag(uint entry)
        {
            return Lua.GetReturnValues("return GetItemCount (\"" + entry.ToString() + "\")", "fish.lua")[0].ToInt32();
        }

        private Stopwatch poolingTimer = new Stopwatch();

        public override void Pulse()
        {

            if (!poolingTimer.IsRunning)
                poolingTimer.Start();

            ConfigForm.linenCount.Text = NumOfItemsInBag(2589).ToString();
            ConfigForm.woolenCount.Text = NumOfItemsInBag(2592).ToString();
            ConfigForm.silkCount.Text = NumOfItemsInBag(4306).ToString();
            ConfigForm.magewaveCount.Text = NumOfItemsInBag(4338).ToString();
            ConfigForm.runeCount.Text = NumOfItemsInBag(14047).ToString();
            ConfigForm.netherCount.Text = NumOfItemsInBag(21877).ToString();
            ConfigForm.frostCount.Text = NumOfItemsInBag(33470).ToString();
            ConfigForm.emberCount.Text = NumOfItemsInBag(53010).ToString();


            if (!ObjectManager.Me.IsAlive || ObjectManager.Me.Combat || ObjectManager.Me.Mounted)
                return;

            if (poolingTimer.ElapsedMilliseconds > 300000)
            {
                while (config.linenBandage.Equals("Heavy") && int.Parse(ConfigForm.linenCount.Text) > 1)
                {
                    while (ObjectManager.Me.IsMoving)
                        WoWMovement.MoveStop();
                    SpellManager.Cast(linen2);
                    Thread.Sleep(2500);
                    ConfigForm.linenCount.Text = NumOfItemsInBag(2589).ToString();
                    poolingTimer.Reset();
                    poolingTimer.Start();
                }

                while (config.linenBandage.Equals("Normal") && int.Parse(ConfigForm.linenCount.Text) > 0)
                {
                    while (ObjectManager.Me.IsMoving)
                        WoWMovement.MoveStop();
                    SpellManager.Cast(linen1);
                    Thread.Sleep(2500);
                    ConfigForm.linenCount.Text = NumOfItemsInBag(2589).ToString();
                    poolingTimer.Reset();
                    poolingTimer.Start();
                }


                while (config.woolenBandage.Equals("Heavy") && int.Parse(ConfigForm.woolenCount.Text) > 1)
                {
                    while (ObjectManager.Me.IsMoving)
                        WoWMovement.MoveStop();
                    SpellManager.Cast(woolen2);
                    Thread.Sleep(2500);
                    ConfigForm.woolenCount.Text = NumOfItemsInBag(2592).ToString();
                    poolingTimer.Reset();
                    poolingTimer.Start();
                }
                while (config.woolenBandage.Equals("Normal") && int.Parse(ConfigForm.woolenCount.Text) > 0)
                {
                    while (ObjectManager.Me.IsMoving)
                        WoWMovement.MoveStop();
                    SpellManager.Cast(woolen1);
                    Thread.Sleep(2500);
                    ConfigForm.woolenCount.Text = NumOfItemsInBag(2592).ToString();
                    poolingTimer.Reset();
                    poolingTimer.Start();
                }

                while (config.silkBandage.Equals("Heavy") && int.Parse(ConfigForm.silkCount.Text) > 1)
                {
                    while (ObjectManager.Me.IsMoving)
                        WoWMovement.MoveStop();
                    SpellManager.Cast(silk2);
                    Thread.Sleep(2500);
                    ConfigForm.silkCount.Text = NumOfItemsInBag(4306).ToString();
                    poolingTimer.Reset();
                    poolingTimer.Start();
                }
                while (config.silkBandage.Equals("Normal") && int.Parse(ConfigForm.silkCount.Text) > 0)
                {
                    while (ObjectManager.Me.IsMoving)
                        WoWMovement.MoveStop();
                    SpellManager.Cast(silk1);
                    Thread.Sleep(2500);
                    ConfigForm.silkCount.Text = NumOfItemsInBag(4306).ToString();
                    poolingTimer.Reset();
                    poolingTimer.Start();

                }


                while (config.mageBandage.Equals("Heavy") && int.Parse(ConfigForm.magewaveCount.Text) > 1)
                {
                    while (ObjectManager.Me.IsMoving)
                        WoWMovement.MoveStop();
                    SpellManager.Cast(mage2);
                    Thread.Sleep(2500);
                    ConfigForm.magewaveCount.Text = NumOfItemsInBag(4338).ToString();
                    poolingTimer.Reset();
                    poolingTimer.Start();
                }
                while (config.mageBandage.Equals("Normal") && int.Parse(ConfigForm.magewaveCount.Text) > 0)
                {
                    while (ObjectManager.Me.IsMoving)
                        WoWMovement.MoveStop();
                    SpellManager.Cast(mage1);
                    Thread.Sleep(2500);
                    ConfigForm.magewaveCount.Text = NumOfItemsInBag(4338).ToString();
                    poolingTimer.Reset();
                    poolingTimer.Start();

                }

                while (config.runeBandage.Equals("Heavy") && int.Parse(ConfigForm.runeCount.Text) > 1)
                {
                    while (ObjectManager.Me.IsMoving)
                        WoWMovement.MoveStop();
                    SpellManager.Cast(rune2);
                    Thread.Sleep(2500);
                    ConfigForm.runeCount.Text = NumOfItemsInBag(14047).ToString();
                    poolingTimer.Reset();
                    poolingTimer.Start();
                }
                while (config.runeBandage.Equals("Normal") && int.Parse(ConfigForm.runeCount.Text) > 0)
                {
                    while (ObjectManager.Me.IsMoving)
                        WoWMovement.MoveStop();
                    SpellManager.Cast(rune1);
                    Thread.Sleep(2500);
                    ConfigForm.runeCount.Text = NumOfItemsInBag(14047).ToString();
                    poolingTimer.Reset();
                    poolingTimer.Start();

                }

                while (config.netherBandage.Equals("Heavy") && int.Parse(ConfigForm.netherCount.Text) > 1)
                {
                    while (ObjectManager.Me.IsMoving)
                        WoWMovement.MoveStop();
                    SpellManager.Cast(nether2);
                    Thread.Sleep(2500);
                    ConfigForm.netherCount.Text = NumOfItemsInBag(21877).ToString();
                    poolingTimer.Reset();
                    poolingTimer.Start();
                }
                while (config.netherBandage.Equals("Normal") && int.Parse(ConfigForm.netherCount.Text) > 0)
                {
                    while (ObjectManager.Me.IsMoving)
                        WoWMovement.MoveStop();
                    SpellManager.Cast(nether1);
                    Thread.Sleep(2500);
                    ConfigForm.netherCount.Text = NumOfItemsInBag(21877).ToString();
                    poolingTimer.Reset();
                    poolingTimer.Start();

                }

                while (config.frostBandage.Equals("Heavy") && int.Parse(ConfigForm.frostCount.Text) > 1)
                {
                    while (ObjectManager.Me.IsMoving)
                        WoWMovement.MoveStop();
                    SpellManager.Cast(frost2);
                    Thread.Sleep(2500);
                    ConfigForm.frostCount.Text = NumOfItemsInBag(33470).ToString();
                    poolingTimer.Reset();
                    poolingTimer.Start();
                }
                while (config.frostBandage.Equals("Normal") && int.Parse(ConfigForm.frostCount.Text) > 0)
                {
                    while (ObjectManager.Me.IsMoving)
                        WoWMovement.MoveStop();
                    SpellManager.Cast(frost1);
                    Thread.Sleep(2500);
                    ConfigForm.frostCount.Text = NumOfItemsInBag(33470).ToString();
                    poolingTimer.Reset();
                    poolingTimer.Start();

                }

                while (config.emberBandage.Equals("Heavy") && int.Parse(ConfigForm.emberCount.Text) > 1)
                {
                    while (ObjectManager.Me.IsMoving)
                        WoWMovement.MoveStop();
                    SpellManager.Cast(ember2);
                    Thread.Sleep(2500);
                    ConfigForm.emberCount.Text = NumOfItemsInBag(53010).ToString();
                    poolingTimer.Reset();
                    poolingTimer.Start();
                }
                while (config.emberBandage.Equals("Normal") && int.Parse(ConfigForm.emberCount.Text) > 0)
                {
                    while (ObjectManager.Me.IsMoving)
                        WoWMovement.MoveStop();
                    SpellManager.Cast(ember1);
                    Thread.Sleep(2500);
                    ConfigForm.emberCount.Text = NumOfItemsInBag(53010).ToString();
                    poolingTimer.Reset();
                    poolingTimer.Start();

                }

            }


            if (ObjectManager.Me.HealthPercent < (double)config.useBandage && !ObjectManager.Me.Auras.ContainsKey("Recently Bandaged") && !ObjectManager.Me.Combat)
            {
                WoWItem bdg = HaveItemCheck(bandageEID);
                if (bdg != null)
                {
                    while (ObjectManager.Me.IsMoving) WoWMovement.MoveStop();

                    Lua.DoString("UseItemByName(\"" + bdg.Name + "\")");

                    Thread.Sleep(1500);

                    while (ObjectManager.Me.Auras.ContainsKey("First Aid"))
                    {
                        Thread.Sleep(50);
                    }

                }
            }

        }



    }
}
